<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{Order, Business, GoodsSpec};
use think\Db;
use app\facade\Redis;

class Message extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Message')->setDescription('订单下单、取消通知');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/garbage/think Message'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        //新订单通知
        (new Order())->where(['status' => 2, 'message' => 1])->chunk(5, function ($lists) {
            foreach ($lists as $list) {
                $first = "您有一个新的【外送】订单";
                if ($list['type'] == 2) {
                    $first = "您有一个新的【自提】订单";
                }
                $data = [
                    'first' => [
                        'value' => $first
                    ],
                    'keyword1' => [
                        'value' => $list['ordersn']
                    ],
                    'keyword2' => [
                        'value' =>  $list['created']
                    ],
                    'keyword3' => [
                        'value' => $list['name']
                    ],
                    'keyword4' => [
                        'value' =>  $list['area'] . $list['getaddress'] . $list['address']
                    ],
                    'remark' => [
                        'value' => "请尽快配货进行服务喔"
                    ]
                ];
                $openid = (new Business())->where('id', $list['businessid'])->value('wopenid') ?? '';
                if (empty($openid)) {
                    (new Order())->where(['id' => $list['id']])->update(['message' => 2]);
                    continue;
                }
                $result = (new Order())->message($openid, 'mDgUsTS1E5HZBWcijYhQZMYiROSjA3j8j4XmLVld8ns', $data, config('wechat.')['app_id'], '/');
                if ($result['errcode'] == 0) {
                    (new Order())->where(['id' => $list['id']])->update(['message' => 2]);
                    echo "新订单已通知";
                } else {
                    echo $result['errmsg'] ?? '';
                }
            }
        });
        //取消订单通知
        (new Order())->whereOr(function ($query) {
            $query->where(['status' => 5, 'message' => [1, 2], 'payment' => 1]);
        })->whereOr(function ($query) {
            $query->where(['status' => [2, 3], 'message' => [1, 2], 'tuikuan' => 2, 'payment' => 1]);
        })->chunk(5, function ($lists) {
            foreach ($lists as $list) {
                $first = "您有一个【外送】订单已取消";
                if ($list['type'] == 2) {
                    $first = "您有一个【自提】订单已取消";
                }
                $data = [
                    'first' => [
                        'value' => $first
                    ],
                    'keyword1' => [
                        'value' => $list['ordersn']
                    ],
                    'keyword2' => [
                        'value' =>  $list['created']
                    ],
                    'remark' => [
                        'value' => "请进行处理"
                    ]
                ];
                $openid = (new Business())->where('id', $list['businessid'])->value('wopenid') ?? '';
                if (empty($openid)) {
                    (new Order())->where(['id' => $list['id']])->update(['message' => 3]);
                    continue;
                }
                $result = (new Order())->message($openid, 's1Gn5yiQV0jPKzFZZiA7nI0jOLqejlfjHDgn9jn5wWI', $data, config('wechat.')['app_id'], '/');
                if ($result['errcode'] == 0) {
                    (new Order())->where(['id' => $list['id']])->update(['message' => 3]);
                    echo "取消订单已通知";
                } else {
                    echo $result['errmsg'] ?? '';
                }
            }
        });
    }
}
